<script>
	import auth from 'common/js/auth.js';
	import config from 'common/js/config.js';
	export default {
		mixins: [auth],
		onLaunch(data) {
			if (uni.getStorageSync('baseUrl') != config.baseUrl) {
				uni.clearStorageSync();
			}
			uni.setStorageSync('baseUrl', config.baseUrl);

			// #ifdef MP
			const updateManager = uni.getUpdateManager();
			updateManager.onCheckForUpdate(function(res) {
				// 请求完新版本信息的回调
			});

			updateManager.onUpdateReady(function(res) {
				uni.showModal({
					title: '更新提示',
					content: '新版本已经准备好，是否重启应用？',
					success(res) {
						if (res.confirm) {
							// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
							updateManager.applyUpdate();
						}
					}
				});
			});

			updateManager.onUpdateFailed(function(res) {
				// 新的版本下载失败
			});
			// #endif

			// uni.getLocation({
			// 	type: 'gcj02',
			// 	success: res => {
			// 		let obj = uni.getStorageSync('location');
			// 		if (obj) {
			// 			let a = this.$util.getDistance(obj.latitude, obj.longitude, res.latitude, res.longitude);
			// 			if (a > 20) {
			// 				uni.removeStorageSync('store');
			// 			}
			// 		}
			// 		uni.setStorage({
			// 			key: 'location',
			// 			data: {
			// 				latitude: res.latitude,
			// 				longitude: res.longitude
			// 			}
			// 		});

			// 		//获取定位区域
			// 		this.$api.sendRequest({
			// 			url: '/api/address/lbsGetCity',
			// 			data: {
			// 				user_latitude: res.latitude,
			// 				user_longitude: res.longitude
			// 			},
			// 			success: res => {
			// 				if (res.code == 0) {
			// 					let data = res.data;
			// 					uni.setStorageSync('city', data['location']);
			// 					uni.setStorageSync('website_id', data['website_id']);
			// 				}
			// 			}
			// 		})
			// 	}
			// });

			// #ifdef APP-PLUS || APP-PLUS-NVUE
			this.checkVersion();  //app监测更新
			// #endif

			// #ifdef H5
			if (uni.getSystemInfoSync().platform == 'ios') {
				uni.setStorageSync('initUrl', location.href);
			}
			// #endif

			// uni.onNetworkStatusChange(function(res) {
			// 	if (!res.isConnected) {
			// 		uni.showModal({
			// 			title: '网络失去链接',
			// 			content: '请检查网络链接',
			// 			showCancel: false
			// 		});
			// 	}
			// });
		},
		onShow: function() {
			this.$store.dispatch('init');
			// 自动登录
			// #ifdef H5
			if (!location.href.includes('plugpages/collection/index') && !location.href.includes('plugpages/collection/result') && !uni.getStorageSync('token') && !uni
				.getStorageSync('loginLock') && !uni.getStorageSync('unbound')) {
				if (this.$util.isWeiXin()) {
					this.$util.getUrlCode(urlParams => {
						if (urlParams.suid) uni.setStorageSync('suid', urlParams.suid);
						if (urlParams.code == undefined) {
							this.$api.sendRequest({
								url: '/wechat/api/wechat/authcode',
								data: {
									redirect_url: location.href
								},
								success: res => {
									if (res.code >= 0) {
										location.href = res.data;
									}
								}
							});
						} else {
							this.$api.sendRequest({
								url: '/wechat/api/wechat/authcodetoopenid',
								data: {
									code: urlParams.code
								},
								success: res => {
									if (res.code >= 0) {
										let data = {};
										if (res.data.openid) data.wx_openid = res.data.openid;
										if (res.data.unionid) data.wx_unionid = res.data.unionid;
										if (res.data.userinfo) Object.assign(data, res.data
											.userinfo);
										this.authLogin(data);
									}
								}
							});
						}
					});
				} else if (this.$util.isAlipay()) { //支付宝内				
					this.$util.getUrlCode(urlParams => {
						if (urlParams.suid) uni.setStorageSync('suid', urlParams.suid);
						if (urlParams.auth_code == undefined) {
							this.$api.sendRequest({
								url: '/alipay/api/alipay/authcode',
								data: {
									redirect_url: location.href
								},
								success: res => {
									if (res.code >= 0) {
										location.href = res.data;
									}
								}
							});
						} else {
							this.$api.sendRequest({
								url: '/alipay/api/alipay/authcodetoopenid',
								data: {
									code: urlParams.auth_code
								},
								success: res => {
									if (res.code >= 0) {
										let data = {};
										if (res.data.openid) data.ali_openid = res.data.openid;
										if (res.data.userinfo) Object.assign(data, res.data
											.userinfo);
										this.authLogin(data);
									}
								}
							});
						}
					});
				}
			}
			// #endif
		},
		onHide: function() {
			this.$store.dispatch('getThemeStyle');
		},
		methods: {
			// #ifdef APP-PLUS || APP-PLUS-NVUE
			// app更新检测
			checkVersion() {
				// 获取应用版本号
				let version = plus.runtime.version;
				console.log(version)
				//检测当前平台，如果是安卓则启动安卓更新
				uni.getSystemInfo({
					success: res => {
						this.updateHandler(res.platform, version);
					}
				})
			},
			// 更新操作
			updateHandler(platform, version) {
				let data = {
					platform: platform,
					version: version
				}
				console.log(data);
				this.$api.sendRequest({
					url: '/app/api/index/getAppVersion',
					data: data,
					success: res => {
						console.log(res)
						if (res.code >= 0) {
							let info = res.data;
							uni.showModal({
								//提醒用户更新
								title: '更新提示',
								content: info.describe,
								showCancel: info.is_force == 1 ? false : true, // 是否显示取消按钮，默认为 true
								success: res => {
									if (res.confirm) {
										plus.runtime.openURL(info.link)
									}
								}
							})
						}
					}
				});
			},
			// #endif
			/**
			 * 授权登录
			 */
			authLogin(data) {
				uni.setStorage({
					key: 'authInfo',
					data: data
				});

				if (uni.getStorageSync('suid')) data.suid = uni.getStorageSync('suid');

				this.$api.sendRequest({
					url: '/api/login/auth',
					data,
					success: res => {
						if (res.code >= 0) {
							uni.setStorage({
								key: 'token',
								data: res.data.token,
								success: () => {
									this.$store.dispatch('getCartNumber');
									this.$store.commit('setToken', res.data.token);
								}
							});
						} else {
							uni.setStorage({
								key: 'unbound',
								data: 1,
								success: () => {}
							});
						}
					}
				});
			}
		}
	};
</script>
<style lang="scss">
	@import url('/common/css/iconfont.css');
	@import './common/css/main.scss';

	/* #ifdef H5 */
	uni-page[data-page='otherpages/index/city/city'] {
		overflow: hidden !important;
	}

	/* #endif */
</style>